<?php
/**手机端购彩大厅**/
namespace app\mobile\controller;
use app\common\controller\Base;
use think\Db;
use GuzzleHttp\json_encode;

class Bet extends Base
{
    protected $chedan;
    protected $zhuihao;
    
    public function _initialize() {
        parent::_initialize();
        $start = array(
                'user_all'                     => $this->user_all,
                'app_down'                 => $this->app_down,
                'logo'                          => $this->logo,
                'now_time_windows' => $this->now_time_windows
        );
        //判断投注功能是否开启
        $zhuh_class_ifopen = Db::name('xt_tzms')->find();
        if($zhuh_class_ifopen['chedan']) {
            $this->chedan = 1;
        } else {
            $this->chedan = 0;
        }
        //判断追号功能是否开启
        if($zhuh_class_ifopen['zhuihao']) {
            $this->zhuihao = 1;
        } else {
            $this->zhuihao = 0;
        }
        
        $this->assign($start);
        $this->assign('cur','shop');
        $this->assign('chedan',$this->chedan);
        $this->assign('zhuihao',$this->zhuihao);
    }

    /**
     * @Purpose 手机版购彩大厅
     * @Time 2017-11
     */
    public function index(){

        $caizhong_list = [];
    	$max_id_query = Db::name('kjjg_list')
					    	->field('MAX(qihao),czid')
					    	->where('kjjg','>','0')
					    	->group('czid')
					    	->buildSql();
    	$max_czid_query = Db::name('kjjg_list')
					    	->field('czid,kjjg,qihao')
					    	->where("(qihao,czid) IN({$max_id_query})")
					    	->buildSql();
    	$periods_query = Db::name('time')
					    	->field('czid,COUNT(*) count')
					    	->group('czid')
					    	->buildSql();

    	$caizhong = Db::name('caipai_list')->alias('a')
    					->join("{$max_czid_query} b",'a.id=b.czid')
    					->join("{$periods_query} tq",'a.id=tq.czid')
    					->field('a.id,a.cp_name_old,a.cp_name_new,a.num_day,b.kjjg,b.qihao,a.cp_dn_index')
    					->where('cp_if_open',1)
    					->order('cp_mb_px','ASC')
    					->select();
		$cp_id = [];
		$trend = [];
    	foreach ($caizhong as $v){
    		$cp_id[] = $v['id'];
    		$v['kjjg']=kjjg_split($v['kjjg'], $v['id']);
    		$a=0;
    		foreach ($v['kjjg'] as $ss){
    		    $a += $ss;
    		}
    		if($v['id']==36 || $v['id']==44){
    			$v['kjjg'][] = '=';
    			$v['kjjg'][] = $a;
    		}
    		$v['qihao'] = substr($v['qihao'], -3)+1;
    		$jztime_fur = Db::name('kjjg_list')->where(['czid'=>$v['id'],'kjjg'=>'0'])->where('jztime','>',time())->order('qihao','DESC')->value('jztime');
    		$jztime_fur = $jztime_fur ? $jztime_fur : time();
    		$jztime_fur = $jztime_fur - time();
    		$v['jztime_fur'] = $jztime_fur;
    		$v['h'] = floor($jztime_fur / 3600);
    		$v['m'] = floor(($jztime_fur / 60) % 60);
    		$v['s'] = floor($jztime_fur % 60);
    		if($v['h'] <10) $v['h'] = '0'.$v['h'];
    		if($v['m'] <10) $v['m'] = '0'.$v['m'];
    		if($v['s'] <10) $v['s'] = '0'.$v['s'];
    		$caizhong_list[$v['id']] = $v;
    		if($v['cp_dn_index'] == 1) $trend[] = $v['id'];
//    		$jztime_fur_arr[] = array('jztime'=>$jztime_fur,'czid'=>$v['id']);
    	}

		$typeAttr = [
				[
						'class_name_lottery' => '全部彩种',
						'czids' => implode(',',$trend),
						'class_sign' => 'rm',
						'select_d' => 'rm_h',
				]
		];
		$type = Db::name("lottery_classify")->field('class_name_lottery,czids,class_sign')->where(['status'=>1])->order("sorting ASC,id asc")->select();
    	foreach ($type as $val){
			if('pk10'==$val['class_sign'])  $val['class_sign'] = 'pk';
			$val['select_d'] = $val['class_sign'];
			$typeAttr[] = $val;
    	}
		$this->assign('caipiao',$caizhong_list);
		$this->assign('big_type',$typeAttr);

		$this->assign('cp_id',json_encode($cp_id));
//    	$this->assign('jztime_fur_json',json_encode($jztime_fur_arr));
    	return $this->fetch('list');
    }
    public function indexa()
    {
    	$way_classify = Db::name('way_classify')->field('lottery_class_id')->select();
    	$classify = array();
    	foreach($way_classify as $value){
    		$classify[]=$value['lottery_class_id'];
    	}
    	$classify = array_unique($classify);
    	$wf_id = '';
    	foreach ($classify as $v){
    		$wanfa = Db::name('lottery_classify')->where('id',$v)->value('czids');
    		$wf_id.=$wanfa.',';
    	}
    	$wf_id = explode(',', $wf_id);
    	
    	$caipai = array();
    	
    	foreach ($wf_id as $id){
    		$cp = Db::name('caipai_list')->where('cp_if_open<2')->where('id',$id)->find();
    		$caipai[] = $cp;
    	}
    	$caipai = array_filter($caipai);
    	
    	foreach ($caipai as &$remsingle){
    		$remsingle['cp_name_new'] || $remsingle['cp_name_new']=$remsingle['cp_name_old'];
    	}

        $remhigete = Db::name('caipai_list')->where('cp_if_open<2')->order('cp_mb_px desc')->select(); 

        foreach($remhigete as &$remsingle)
        {
            $remsingle['cp_name_new'] || $remsingle['cp_name_new']=$remsingle['cp_name_old'];
        }
		$this->assign('caipai',$caipai);
        $this->assign('remhigete',$remhigete);

        return $this->fetch('list_a');
    }
    
    /**
     * @Purpose 手机版购彩大厅所有彩种信息
     * @Time 2017-11
     */
    public function lotteryList()
    {
        $caipai_list = new \app\index\logic\CaipaiList();
        $openclsit = $caipai_list->getMallLotteryDetail();
        $this->assign('all_caipiao',$openclsit);
        return $this->fetch('lottery_list');
    }

    
}
